#!/usr/bin/env python3.3

#
#  Filename: e92.py
#  Author  : lukas singer
#
#  Comment : Created by createSolution.sh
#

#import euler
#from euler import *

l1=set([1])
l89=set([89])
lsqrs=dict([(csqr,(int(csqr)**2)) for csqr in "0123456789"])
def isHappy(n):
  t=set()
  m=n
  while not((m in l1)or(m in l89)):
    t.add(m)
    m=sum(lsqrs[c] for c in str(m))
  if m in l1:
    l1.union(t)
    return False
  else:
    l89.union(t)
    return True


def pe92():
  target=int(1e7)
  target=int(1e4)
  x=0
  for i in range(1,targe):
    if isHappy(i):
      x+=1
  print(x)

if __name__=="__main__":
  pe92()

